
local NameSettingLayer=class("NameSettingLayer",FZutil.createUILayer)

function NameSettingLayer:ctor()
    self:enableNodeEvents()
	local uimap=FZutil.createSceneNode("GeneralUIall/GeneralUIView_setName")
	self.uimap=uimap
	self:addChild(uimap.root)
    uimap.root:setPosition(display.center)	

    uimap.TextField_input:onEvent(function(e)
        if e.name=="ATTACH_WITH_IME" then
            uimap.root:moveTo({y=display.cy+100,time=0.3})
        elseif e.name=="DETACH_WITH_IME" then
            uimap.root:moveTo({y=display.cy,time=0.3})
        end
    end)
    
    uimap.Button_random:onTouch(function(e)
        if e.name=="ended" then
        	self:RandomOneName()
        end
    end)
    
    uimap.Button_confirm:onTouch(function(e)
        if e.name=="ended" then
            self._name=uimap.TextField_input:getString()
            local msg={rolename=self._name,uid=RoleData.player_info.uid,sex=RoleData.player_info.sex}
            NetEngine:sendMessage(ProtoCmd.ReqCreateRoleMessage,msg)
        end
    end)
    uimap.Text_random_N:hide()
    uimap.TextField_input:setMaxLength(8)
    self:RandomOneName()
end

function NameSettingLayer:RandomOneName()
	local txt=self.uimap.TextField_input
    local rand = os.time()%100
    self._name=self:randomNewName()
	txt:setString(self._name)
end

-- 随机名字
local nan = 1 -- 计数器
function NameSettingLayer:randomNewName()
--    local table1 = configData.st_player_name1
--    local name1 = {}
--    for key, var in pairs(table1) do
--        if type(var)=="table" then
--            table.insert(name1,key)
--        end
--    end
--    table.sort(name1)

--    local table2 = configData.st_player_name2
--    local name2 = {}
--    for key, var in pairs(table2) do
--        if type(var)=="table" then
--            table.insert(name2,key)
--        end
--    end
--    table.sort(name2)

--    local table3 = configData.st_player_name3
--    local name3 = {}
--    for key, var in pairs(table3) do
--        if type(var)=="table" then
--            table.insert(name3,key)
--        end
--    end
--    table.sort(name3)

--    local table4 = configData.st_player_name4
--    local name4 = {}
--    for key, var in pairs(table4) do
--        if type(var)=="table" then
--            table.insert(name4,key)
--        end
--    end
--    table.sort(name4)

--    local t1 = math.random(1, #name1)
--    local t2 = math.random(1, #name2)
--    local t3 = math.random(1, #name3)
--    local t4 = math.random(1, #name4)

--    local newName
--    if nan == 1 then
--        newName = name1[t1] .. name2[t2] .. name4[t4]
--        nan = nan + 1
--    else
--        newName = name1[t1] .. name3[t3] .. name4[t4]
--        nan = nan - 1
--    end

--    print("随机组合名字： ".. newName)
    local table1 = configData.name
    local name1 = {}
    for key, var in pairs(table1) do
        if type(var)=="table" then
            table.insert(name1,var["name"])
        end
    end
    table.sort(name1)
    local t1 = math.random(1, #name1)
    return name1[t1]
    --return newName
end

function NameSettingLayer:onEnter()

end

function NameSettingLayer:onExit()

end

return NameSettingLayer